<?php
/**
 * 手机端分类图片设置
 *
 *
 *
 * @好商城提供技术支持 授权请购买shopnc授权
 * @license    http://www.33hao.com
 * @link       交流群号：138182377
 */



defined('In33hao') or exit('Access Invalid!');
class qrcode_settingControl extends SystemControl{

    private $links = array(
        array('url'=>'act=qrcode_setting&op=indexBg','text'=>'用户推广背景设置'),
        array('url'=>'act=qrcode_setting&op=storeBg','text'=>'小店推广背景设置'),
    );

    public function __construct(){
        parent::__construct();
        Language::read('mobile');
    }

    public function indexOp() {
        $this->indexBgOp();
    }

    /**
     * 小店推广背景
     */
    public function indexBgOp() {
        $dir = "user";

        if(chksubmit()){
            $upload = new UploadFile();
            $upload->set('default_dir',ATTACH_MOBILE."/qrcodebg/".$dir);
            $result = $upload->upfile('picbg');
            if ($result){
                $olderFile = ATTACH_MOBILE."/qrcodebg/".$dir.'/'.$upload->file_name;
                $newFile = ATTACH_MOBILE."/qrcodebg/".$dir.'/'."qrcode.jpg";
                unlink(BASE_UPLOAD_PATH.'/'.$newFile);
                $is =  copy(BASE_UPLOAD_PATH.'/'.$olderFile,BASE_UPLOAD_PATH.'/'.$newFile);
                unlink(BASE_UPLOAD_PATH.'/'.$olderFile);
                showMessage("设置成功");
            }else {
                showMessage($upload->error);
            }
        }
        $default = UPLOAD_SITE_URL.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg";
        $path =  UPLOAD_SITE_URL.'/'.ATTACH_MOBILE."/qrcodebg/".$dir."/qrcode.jpg";
        if(!file_exists(BASE_UPLOAD_PATH.'/'.ATTACH_MOBILE."/qrcodebg/".$dir."/qrcode.jpg")){
            $path = UPLOAD_SITE_URL.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg";
        }
        //输出子菜单
        Tpl::output('top_link',$this->sublink($this->links,'indexBg'));
        Tpl::output('defaulte',$default);
        Tpl::setDirquna('mobile');
        Tpl::output('path',$path);
        Tpl::showpage('qr_bg.setting');
    }

    /**
     * 小店推广背景
     */
    public function storeBgOp() {
        $dir = "store";
        if(chksubmit()){
            $upload = new UploadFile();
            $upload->set('default_dir',ATTACH_MOBILE."/qrcodebg/".$dir);
            $result = $upload->upfile('picbg');
            if ($result){
                $olderFile = ATTACH_MOBILE."/qrcodebg/".$dir.'/'.$upload->file_name;
                $newFile = ATTACH_MOBILE."/qrcodebg/".$dir.'/'."qrcode.jpg";
                unlink(BASE_UPLOAD_PATH.'/'.$newFile);
                $is =  copy(BASE_UPLOAD_PATH.'/'.$olderFile,BASE_UPLOAD_PATH.'/'.$newFile);
                unlink(BASE_UPLOAD_PATH.'/'.$olderFile);
                showMessage("设置成功");
            }else {
                showMessage($upload->error);
            }
        }
        $default = UPLOAD_SITE_URL.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg";
        $path =  UPLOAD_SITE_URL.'/'.ATTACH_MOBILE."/qrcodebg/".$dir."/qrcode.jpg";
        if(!file_exists(BASE_UPLOAD_PATH.'/'.ATTACH_MOBILE."/qrcodebg/".$dir."/qrcode.jpg")){
            $path = UPLOAD_SITE_URL.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg";
        }
        //输出子菜单
        Tpl::output('top_link',$this->sublink($this->links,'storeBg'));
        Tpl::output('defaulte',$default);
        Tpl::setDirquna('mobile');
        Tpl::output('path',$path);
        Tpl::showpage('qr_store_bg.setting');
    }

    /**
     * 下载图片
     */
    public function downOp(){
        $default = UPLOAD_SITE_URL.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg";
        if(!file_exists(BASE_UPLOAD_PATH.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg")){
            showMessage("图片不存在","index.php?act=qrcode_setting&op=index");
        }
        $fp=fopen(BASE_UPLOAD_PATH.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg","r");
        $file_size=filesize(BASE_UPLOAD_PATH.'/'.ATTACH_MOBILE."/qrcodebg/defaulte/qrcode.jpg");
        //下载文件需要用到的头
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:".$file_size);
        Header("Content-Disposition: attachment; filename="."默认图片.jpg");
        $buffer=1024;
        $file_count=0;
//向浏览器返回数据
        while(!feof($fp) && $file_count<$file_size){
            $file_con=fread($fp,$buffer);
            $file_count+=$buffer;
            echo $file_con;
        }
        fclose($fp);
    }

}
